package com.epfresh.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.utils.ToastUtils;
import com.epfresh.R;
import com.epfresh.api.constant.Constant;
import com.epfresh.global.BaseActivity;
import com.epfresh.global.BaseApplication;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.util.Hashtable;

public class AboutActivity extends BaseActivity {

    static final String TAG = "AboutActivity";
    private TextView now_version;
    private ImageView apk_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        now_version = (TextView) findViewById(R.id.now_version);
        apk_code = (ImageView) findViewById(R.id.apk_code);
        initView();

    }

    private void initView() {
        ivBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("关于我们");

        findViewById(R.id.protocol).setOnClickListener(this);
        PackageManager manager;
        PackageInfo info = null;
        manager = this.getPackageManager();
        //createImage();
        try {
            info = manager.getPackageInfo(this.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if(info!=null)
            now_version.setText("当前版本V" + info.versionName);
        else
            now_version.setText("您可以检查新版本");
        createImage();// TODO: 2016/7/4
/*		ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
            @Override
			public void getOutline(View view, Outline outline) {
				//int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
				outline.setOval(2, 2, 5, 5);
			}
		};
		apk_code.setOutlineProvider(viewOutlineProvider);*/
	/*	info.versionCode;

		info.versionName;

		info.packageName;

		info.signatures;*/

    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.baseback:
                // 返回上一页
                finish();
                break;
            case R.id.find_new:
                // 返回上一页
                toast(BaseApplication.getDoc().getLatestVersion());
                break;
            case R.id.protocol:
                help();
                break;
            default:
                break;
        }
    }

    private void help() {

        Intent intent = new Intent(this, WebActivity.class);
        intent.putExtra(Constant.KEY_WEB_URL, Constant.URL_HELP_PUR_AGREEMENT);
        intent.putExtra(Constant.KEY_WEB_TITLE, "软件使用协议");
        startActivity(intent);

    }

    private void createImage() {
        try {
            // 需要引入core包
//			QRCodeWriter writer = new QRCodeWriter();
            String text = Constant.URL_SHARE_DOWNLOAD;
            if (text == null || "".equals(text) || text.length() < 1) {
                return;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //hints.put(EncodeHintType.MARGIN, 1);
            int w = LocalDisplay.dp2px(120);
            if (w < 100) {
                w = 100;
            }
            BitMatrix bitMatrix = new QRCodeWriter().encode(text,
                    BarcodeFormat.QR_CODE, w, w, hints);
            int[] pixels = new int[w * w];

            for (int y = 0; y < w; y++) {
                for (int x = 0; x < w; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * w + x] = 0xff000000;
                    } else {
                        pixels[y * w + x] = 0xffffffff;
                    }

                }
            }
            Bitmap bitmap = Bitmap.createBitmap(w, w,
                    Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, w, 0, 0, w, w);
//			Bitmap bitmapTest = bitmap.createBitmap(bitmap, 0, 0, w, w);//按默认返回的图像调整页面边距
            apk_code.setImageBitmap(bitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
}
